<?php

defined('SYSPATH') or die('No direct access allowed.');

/**
 * Service Group
 */
class Model_Service_Group extends Domain_Dao_Group {

    public $_rep;

    /**
     * 
     */
    public function __construct()
    {
        $o_repository_manager = new Domain_Repositories_RepositoryManager;
        $this->_rep = $o_repository_manager->getGroupRepository();
    }

    /**
     *
     * @return <type>
     */
    public function load($id)
    {
        return $this->_rep->load($id);
    }

    /**
     *
     * @param <type> $offset
     * @param <type> $limit
     * @param <type> $filter
     * @param <type> $sort
     * @param <type> $count
     * @return <type>
     */
    public function load_all($is_count = false, $offset=null, $limit=null, $filter = null, $sort = null)
    {
        return $this->_rep->load_all($offset, $limit, $filter, $sort, $is_count);
    }

    /**
     *
     * @param Domain_Entities_User $o_entity
     * @return <type> 
     */
    public function insert($o_entity)
    {
        return $this->_rep->insert_by($o_entity);
    }

    /**
     *
     * @param Domain_Entities_User $o_entity
     * @return <type>
     */
    public function update($o_entity)
    {
        return $this->_rep->update_by($o_entity);
    }

    /**
     *
     * @param <type> $id
     * @return <type> 
     */
    public function delete($id)
    {
        return $this->_rep->delete_by($id);
    }

    public static function name_available_add($name)
    {
        $s_service = new Model_Service_Group;
        $s_service->_rep->clear();
        $s_service->_rep->builder_select('name', array('where' => '='), trim(($name)));

        $n_count = $s_service->_rep->get_count();
        return $n_count <= 0 ? true : false;
    }

    public static function name_available_edit($o_entity)
    {
        $s_service = new Model_Service_Group;
        $s_service->_rep->clear();
        $s_service->_rep->builder_select('name', array('where' => '='), trim($o_entity->getName()));

        $s_service->_rep->builder_select('id', array('where' => '!='), trim($o_entity->getId()));

        $n_count = $s_service->_rep->get_count();
        return $n_count <= 0 ? true : false;
    }

    /**
     *
     * @param <type> $username
     * @param <type> $password
     * @param <type> $file
     * @return <type> 
     */
    public function vaild_add($o_entity, $file)
    {
        $this->_rep->clear();
        $this->_rep->values(Arr::map('trim', array('name' => $o_entity->getName()), array('name')));
        $o_valid = $this->_rep->validation();
        $o_valid->rule('name', 'not_empty');

        if (!$o_valid->check())
        {
            $errors = $o_valid->errors($file);
            return $errors['name'];
        }

        $o_valid->bind(':model', $o_entity->getName());
        $o_valid->rule('name', 'Model_Service_Group::name_available_add', array(':model'));
        if (!$o_valid->check())
        {
            $errors = $o_valid->errors($file);
            return $errors['name'];
        }
        return "";
    }

    /**
     *
     * @param <type> $username
     * @param <type> $password
     * @param <type> $file
     * @return <type>
     */
    public function vaild_edit($o_entity, $file)
    {
        $this->_rep->clear();
        $this->_rep->values(Arr::map('trim', array('name' => $o_entity->getName()), array('name')));
        $o_valid = $this->_rep->validation();
        $o_valid->rule('name', 'not_empty');

        if (!$o_valid->check())
        {
            $errors = $o_valid->errors($file);
            return $errors['name'];
        }

        $o_valid->bind(':model', $o_entity);
        $o_valid->rule('name', 'Model_Service_Group::name_available_edit', array(':model'));
        if (!$o_valid->check())
        {
            $errors = $o_valid->errors($file);
            return $errors['name'];
        }
        return "";
    }

    public function delete_by_ids($s_id)
    {
        $obj = $this->_rep->db_delete($this->_rep->table_name())
                ->where('id', 'in', DB::expr('(' . $s_id . ')'));
        return $this->_rep->db_execute($obj);
    }
     public function get_group_by_location_id($location_id, $offset = null , $limit = null , $search = null , $count = false)
    {
        $this->_rep->clear();
        $this->_rep->builder_select('location_id', array('where' => '='),$location_id);
        if($search != null)
        {
            $this->_rep->builder_select('name', array('where'=> 'like'),$search);
        }
        $this->_rep->builder_select('id', array('order by' =>'ASC' , 'limit'=> $limit, 'offset'=>$offset));
        if($count)
        {
            return $this->_rep->get_count();
        }
        else{
            return $this->_rep->query_all();
        }
    }
    public function get_group_by_name($name,$count = false)
    {
        $this->_rep->clear();
        $this->_rep->builder_select('name', array('where' =>'='),$name);
        if($count)
        {
            return $this->_rep->get_count();
        }
        
        else{
            return $this->_rep->query_all();
        }
    }
}

?>
